package com.example.springbootshiro.shiro;

import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfiguration
{
    @Bean("credentirlMatcher")
    public CredentirlMatcher credentirlMatcher()
    {
        return new CredentirlMatcher();
    }

    @Bean("authRealm")
    public AuthRealm authRealm(@Qualifier("credentirlMatcher") CredentirlMatcher credentirlMatcher)
    {
        AuthRealm authRealm = new AuthRealm();

        //默认缓存到内存
        authRealm.setCacheManager(new MemoryConstrainedCacheManager());

        authRealm.setCredentialsMatcher(credentirlMatcher);

        return authRealm;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm)
    {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        defaultWebSecurityManager.setRealm(authRealm);

        return defaultWebSecurityManager;
    }

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager securityManager)
    {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //定义登录url
        shiroFilterFactoryBean.setLoginUrl("/login");
        //定义登录成功后跳转的url
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //没有权限时跳转的url
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");

        LinkedHashMap<String, String> filterChainDefinitedMap = new LinkedHashMap<>();

        filterChainDefinitedMap.put("/index", "authc");

        filterChainDefinitedMap.put("/login", "anon");

        filterChainDefinitedMap.put("/loginUser", "anon");

        filterChainDefinitedMap.put("/admin", "roles[admin]");

        filterChainDefinitedMap.put("/edit", "perms[edit]");

       // filterChainDefinitedMap.put("/druid/**", "anon");

        filterChainDefinitedMap.put("/**", "user");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitedMap);

        return shiroFilterFactoryBean;
    }

    //shiro和spring关联
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager)
    {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();

        advisor.setSecurityManager(securityManager);

        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator()
    {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();

        proxyCreator.setProxyTargetClass(true);

        return proxyCreator;
    }

}
